/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package anton.menu;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author johan
 */
@ManagedBean(name="create")
@SessionScoped

public class CreateDinnerMenu {
    
    //managed property used to inser a menuname i database
   
    @ManagedProperty(value="#{createMenuName}")
    
    private CreateDinnerMenuName createMenuName;
    
    
     @ManagedProperty(value="#{listMenu}")
    
    private ListDinnerMenu listMenu;
    
     
     // managed property use to get list and selected values related to a specific menu
     @ManagedProperty(value="#{listDinnerNames}")
    
     private ListDinnerNames listDinnerNames;
     
     @ManagedProperty(value="#{destroy}")
     
     private Destroy destroy;
     
     
     
     
   
    private String menuName;
   
    private Integer fineMenuId;
    
    private String fineMenuName;

    private List<String>  selectedValues;

    public Destroy getDestroy() {
        return destroy;
    }

    public void setDestroy(Destroy destroy) {
        this.destroy = destroy;
    }
    
    
    
       
    
    
    public Integer getFineMenuId() {
        return fineMenuId;
    }

  
      public void setFineMenuId(Integer fineMenuId) {
        this.fineMenuId = fineMenuId;
    }
    
    
    public String getFineMenuName() {
        return fineMenuName;
    }

    public void setFineMenuName(String fineMenuName) {
        this.fineMenuName = fineMenuName;
    }    
    
    
    public ListDinnerMenu getListMenu() {
        return listMenu;
    }

    public void setListMenu(ListDinnerMenu listMenu) {
        this.listMenu = listMenu;
    }
    
    
    public CreateDinnerMenuName getCreateMenuName() {
        return createMenuName;
    }

    public void setCreateMenuName(CreateDinnerMenuName createMenuName) {
        this.createMenuName = createMenuName;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public ListDinnerNames getListDinnerNames() {
        return listDinnerNames;
    }

    public void setListDinnerNames(ListDinnerNames listDinnerNames) {
        this.listDinnerNames = listDinnerNames;
    }

  

  
    
    
    
    public String createName() throws SQLException
    {
     String sql=" insert into FineMenu (finemenu_name) values(?)";
    
     createMenuName.setSql(sql);
     
     createMenuName.setMenuName(menuName);
     
     createMenuName.add();
     
     this.menuName=null;
    
     return "/admin/dinnermenu/createmenu?faces-redirect=true";
    }
    
    
    
     public ArrayList<DinnerMenu> getMenuList() throws SQLException
   {      
        
      String sqlMenu="select finemenu_Id, finemenu_name from FineMenu";
       
       listMenu.setSql(sqlMenu);
       
       ArrayList<DinnerMenu> temp=listMenu.getMenu();
       
       return temp;
   }
    
    
     
      public String prepareCreate() 
            
    {  
         
        return "/admin/dinnermenu/createmenu?faces-redirect=true";
    
    }
     
      
     
      public List<String> getDinnerNameList() throws SQLException
      {
      
      String sql = "select dinner_name from Dinner";
      List<String> list= listDinnerNames.getDinnerNameList(sql); 
      return list;
      }
      
      
      
     public  String add() throws SQLException {
         
     
     String sql= "insert into FineDinnerMenu (dinner_name,finemenu_id) values (?,?);";
     
     
     String delete="delete from FineDinnerMenu where finemenu_ID="+fineMenuId;
        
     destroy.destroy(delete);
     
     fineMenuId=null;
     
    
    
    
      
      return "/admin/dinnermenu/createmenu?faces-redirect=true";
     }
     
     
     
     
     public String destroy(Integer fineMenuId) throws SQLException
     {
      String  sql="delete from FineDinnerMenu where finemenu_ID="+fineMenuId.toString()+";";  
      
      destroy.destroy(sql);
       
      sql="delete from FineMenu where finemenu_ID="+fineMenuId.toString()+";";
      
      destroy.destroy(sql);
        
     
     return "/admin/dinnermenu/createmenu?faces-redirect=true";
     }
     
     
     
    
}
